package com.ruoyi.classai.controller;

import com.ruoyi.classai.domain.EwcResource;
import com.ruoyi.classai.domain.dto.resource.EwcResourceAddDto;
import com.ruoyi.classai.domain.dto.resource.EwcResourceQueryDto;
import com.ruoyi.classai.service.IEwcResourceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 资源库Controller
 *
 * @author lcj
 * @date 2025-03-16
 */
@RestController
@RequestMapping("/resource")
public class EwcResourceController extends BaseController {
    @Autowired
    private IEwcResourceService ewcResourceService;

    /**
     * 分页查询资源库列表
     */
    @RequiresPermissions("classai:resource:list")
    @GetMapping("/list")
    public TableDataInfo list(EwcResourceQueryDto dto) {
        startPage();
        List<EwcResource> list = ewcResourceService.selectResourceList(dto);
        return getDataTable(list);
    }

//    /**
//     * 导出资源库列表
//     */
//    @RequiresPermissions("classai:resource:export")
//    @Log(title = "资源库", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, EwcResource ewcResource) {
//        List<EwcResource> list = ewcResourceService.list(Wrappers.query(ewcResource));
//        ExcelUtil<EwcResource> util = new ExcelUtil<EwcResource>(EwcResource.class);
//        util.exportExcel(response, list, "资源库数据");
//    }

    /**
     * 获取资源库详细信息
     */
    @RequiresPermissions("classai:resource:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success("查询成功", ewcResourceService.getResourceById(id));
    }

    /**
     * 新增资源库
     */
    @RequiresPermissions("classai:resource:add")
    @Log(title = "资源库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated EwcResourceAddDto dto) {
        ewcResourceService.insertResource(dto);
        return AjaxResult.success("操作成功！");
    }

    /**
     * 修改资源库
     */
    @RequiresPermissions("classai:resource:edit")
    @Log(title = "资源库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid EwcResourceAddDto dto) {
        ewcResourceService.updateResource(dto);
        return AjaxResult.success("操作成功！");
    }

    /**
     * 删除资源库
     */
    @RequiresPermissions("classai:resource:remove")
    @Log(title = "资源库", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = Arrays.asList(ids);
        ewcResourceService.deleteResourceByIds(list);
        return AjaxResult.success("操作成功！");
    }

    /**
     * 查询资源库列表
     *
     * @param dto
     * @return
     */
    @RequiresPermissions("classai:resource:resourceList")
    @PostMapping("/resourceList")
    public AjaxResult resourceList(@RequestBody EwcResourceQueryDto dto) {
        List<EwcResource> list = ewcResourceService.selectResourceList(dto);
        return AjaxResult.success("操作成功！", list);
    }
}
